/**
 * Add Run to current user
 * 
 * @param args.runDate - Format: 31-12-2012
 * @param args.runType - Values: [RUNNING, CYCLING, SWIMMING]
 * @param args.runDistance
 * @param args.runUrl
 */
function main(){
	var runDateString = json.get("runDate");
	var runDayString = json.get("runDay");
	var runType = json.get("runType");
	var runDistanceString = json.get("runDistance");
	var runUrl = json.get("runUrl");
	
	//TODO test madatory/validity params
	if(isNumber(runDistanceString)){
		var runDate = new Date(); 
		if(runDateString!= null && runDateString!= ""){
			var dateArray = runDateString.split("-");
			runDate = new Date(dateArray[2], dateArray[1], dateArray[0]);
		}
		else if (runDayString != null && runDayString != ""){
			var runDay = parseFloat(runDayString);
			runDate = new Date();
			switch(runDay){
			case 1:
				runDate.setDate(runDate.getDate() - 1);
				break;
			case 2:
				runDate.setDate(runDate.getDate() - 2);
				break;
			case 3:
				runDate.setDate(runDate.getDate() - 3);
				break;
			case 4:
				runDate.setDate(runDate.getDate() - 4);
				break;
			case 5:
				runDate.setDate(runDate.getDate() - 5);
				break;
			case 6:
				runDate.setDate(runDate.getDate() - 6);
				break;
			default:
				break;
			}
		}
			
		var runDistance = parseFloat(runDistanceString);
		jsRunnerService.addRun(runDate, runDistance, runType, runUrl);		
	}
	else{
		status.code = 400;
		status.message = "Distance is not an integer";
		status.redirect = true;
	}
}

/**
 * Check if the value is a number
 * 
 * @param value
 */
function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}
main();